from watermark_build.code.main.config.template_config import *
from PIL import Image, ImageDraw, ImageFilter


if __name__ == '__main__':
    # 打开图片
    original_image = Image.open(UPLOAD_PATH)

    # 获取图片大小
    width, height = original_image.size
    background_width = width + WIDTH_MARGIN
    background_height = height + HEIGHT_MARGIN
    # 创建一个白色的背景图片
    background = Image.new('RGB', (background_width, background_height), 'white')

    # 创建圆形蒙版
    radius = 150  # 圆角半径
    mask = Image.new('L', original_image.size, 0)
    draw = ImageDraw.Draw(mask)
    draw.rounded_rectangle((0, 0, original_image.width, original_image.height), radius, fill=255)

    # 应用圆形蒙版到原始图片上
    rounded_image = Image.composite(original_image.convert('RGBA'),
                                    Image.new('RGBA', original_image.size, (0, 0, 0, 0)), mask)

    # 添加阴影效果
    shadow_offset = (100, 100)  # 阴影偏移量
    shadow_blur = 10  # 阴影模糊程度
    shadow_color = (0, 0, 0, 50)  # 阴影颜色和透明度

    # 创建阴影图片
    shadow_image = Image.new('RGBA', rounded_image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(shadow_image)
    draw.rounded_rectangle((0, 0, rounded_image.width, rounded_image.height), radius, fill=shadow_color)

    # 应用高斯模糊
    shadow_image = shadow_image.filter(ImageFilter.GaussianBlur(shadow_blur))

    # 计算粘贴位置
    paste_position = (
        MARGIN,
        MARGIN
    )

    shadow_paste_position = (
        MARGIN + shadow_offset[0],
        MARGIN + shadow_offset[1]
    )
    # 将阴影图片粘贴到背景图片上
    background.paste(shadow_image, shadow_paste_position, mask=shadow_image)

    # 将原始图片粘贴到背景图片上
    # 将处理后的图片粘贴到背景图片上
    background.paste(rounded_image, paste_position, mask=rounded_image)

    # 加载索尼Logo
    logo_path = template_config[SONY]
    logo_image = Image.open(logo_path)
    logo_width, logo_height = logo_image.size
    logo_position = (
        MARGIN + (background_width - MARGIN * 2 - logo_width) // 2,
        MARGIN + height + MARGIN // 3
    )
    background.paste(logo_image, logo_position, mask=logo_image)

    # 显示或保存最终图片
    background.show()
    # 或者保存图片
    background.save('output_image_with_logo_and_text.jpg')